<?php

require_once "Zend/Tokenizer/Serialized/Abstract.php";

/**
 * @category   Zend
 * @package    Zend_Tokenizer
 * @copyright  Salomon BRYS
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Zend_Tokenizer_Serialized_PHP extends Zend_Tokenizer_Serialized_Abstract
{
	/**
	 * @var bool
	 */
	protected $_unserializedSucceeded;

	/**
	 * @var callback
	 */
	protected $_previousErrorHandler = null;

	/**
	 * Constructor
	 *
	 * @param Zend_Tokenizer_Interface $producer
	 */
	public function __construct(Zend_Tokenizer_Interface $producer, $reSerializeToGetLen = true)
	{
		parent::__construct($producer, $reSerializeToGetLen);
	}

	final protected function unserialize($data, &$token)
	{
		$this->_unserializedSucceeded = true;
		$this->_previousErrorHandler = set_error_handler(array($this, 'Zend_Tokenizer_Serialized_PHP::unserialize_error_handler'));
		$token = @unserialize($data);
		restore_error_handler();
		$this->_previousErrorHandler = null;
		return $this->_unserializedSucceeded;
	}

	final protected function serialize($obj)
	{
		return serialize($obj);
	}

	final protected function unserialize_error_handler($errno, $errstr, $errfile, $errline, $errcontext)
	{
		if ($errno == 8) {
			$this->_unserializedSucceeded = false;
			return TRUE;
		}
		if ($this->_previousErrorHandler)
			call_user_func($this->_previousErrorHandler, $errno, $errstr, $errfile, $errline, $errcontext);
		return FALSE;
	}
}
